# -*- conding:UTF-8 -*-

'''
@ Project: P7_lx1
@ File: user.py
@ Author: 罗婷
@ Date：2022/6/7 14:05
存放用户表
'''
from datetime import datetime

from models import db

# 一对多
#     class User(db.Model):
#         __tablename__ = 'tb_user'
#         uid = db.Column(db.Integer, doc='用户id')
#         name = db.Column(db.String(33), doc='用户名')
#         phone = db.Column(db.String(11), doc='手机号')
#         role_id = db.Column(db.Integer, db.ForeignKey('tb_role.rid'), doc='角色外键')
#
#     class Role(db.Model):
#         __tablename__ = 'tb_role'
#         rid = db.Column(db.Integer, doc='角色id')
#         name = db.Column(db.String(33), doc='角色名')
#
#         r_user = db.relationship('User', backref='roles')




# 用户表
class UserModel(db.Model):
    """
    一对多关系: 用户与角色  User Role
    角色1 用户多
    定义: 在1处定义关系 多处定义外键
    一 role: 映射关系
    r_user = db.relationship(
        "User",  # 映射表格
        backref="roles"  # 自定义名称 用于用户反查角色
    )
    多user:  声明外键字段  对应角色表名的id字段
    role_id = db.Column(db.Integer, db.ForeignKey("role.rid"))
    """
    __tablename__ = 'tb_user'

    class STATUS:
        ENABLE = 1
        DISABLE = 0

    uid = db.Column(db.Integer, primary_key=True, autoincrement=True,doc='用户ID')
    mobile = db.Column(db.String(32), doc='手机号')
    password = db.Column(db.String(256), doc='密码')
    uname = db.Column(db.String(32), doc='昵称')
    gender = db.Column(db.Integer, default=1, doc='1男0女')
    profile_photo = db.Column(db.String(256), nullable=True, doc='头像')
    created_time = db.Column(db.DateTime, default=datetime.now(),doc='创建时间')
    last_login = db.Column(db.DateTime,default=datetime.now(), doc='最后登录时间')
    is_media = db.Column(db.Boolean, default=False, doc='是否是自媒体')
    is_verified = db.Column(db.Boolean, default=False, doc='是否实名认证')
    introduction = db.Column(db.String(128),default='这个人很懒,什么都没有留下', doc='简介')
    certificate = db.Column(db.String(64), default='已认证' ,doc='认证')
    article_count = db.Column(db.Integer, default=0, doc='发帖数')
    following_count = db.Column(db.Integer, default=0, doc='关注的人数')
    fans_count = db.Column(db.Integer, default=0, doc='被关注的人数（粉丝数）')
    like_count = db.Column(db.Integer, default=0, doc='累计点赞人数')
    read_count = db.Column(db.Integer, default=0, doc='累计阅读人数')
    account = db.Column(db.String(32), doc='账号', unique=True)
    email = db.Column(db.String(64),nullable=True, doc='邮箱')
    status = db.Column(db.Integer, default=1, doc='状态，1可用0不可用是否可用')
    superuser = db.Column(db.Integer, default=0, doc='0普通用户 1 管理员 2 超级管理员')
    news = db.relationship('News', backref="news", uselist=False)
    comment = db.relationship('Comment', backref=db.backref('comments'), uselist=False)
    search = db.relationship('SearchHistory', backref=db.backref('searches'), uselist=False)


# 用户关注关系表
class Relation(db.Model):
    __tablename__ = 'tb_user_relation'
    user_id = db.Column(db.Integer, db.ForeignKey("tb_user.uid"), primary_key=True)
    foller_id = db.Column(db.Integer, db.ForeignKey("tb_user.uid"), primary_key=True)
    relation = db.Column(db.Boolean, doc='状态(1,关注;0, 取消)')
    create_time = db.Column(db.DateTime, default=datetime.now(), doc='创建时间')
    update_time = db.Column(db.DateTime, default=datetime.now(), onupdate=datetime.now(), doc='更新时间')
    user = db.relationship('UserModel', foreign_keys=user_id)
    foller = db.relationship('UserModel', foreign_keys=user_id)


#  用户与频道关系表
class UserChannel(db.Model):
    __tablename__ = 'user_channel'
    uid = db.Column(db.Integer, db.ForeignKey('tb_user.uid'), primary_key=True, doc="用户ID")
    cid = db.Column(db.Integer, db.ForeignKey('news_channel.cid'), primary_key=True, doc="频道ID")
    is_delete = db.Column(db.Boolean, default=0, doc="状态(0, 可用;1, 不可用)")
    create_time = db.Column(db.DateTime, default=datetime.now(), doc="创建时间")
    update_time = db.Column(db.DateTime, default=datetime.now(), doc="更新时间")
    sequence = db.Column(db.Integer, default=0, doc='序号')


# 频道表
class Channel(db.Model):
    __tablename__ = 'news_channel'

    cid = db.Column(db.Integer, primary_key=True, doc='频道ID')
    cname = db.Column(db.String(32), doc='频道名称')
    ctime = db.Column(db.DateTime, default=datetime.now(), doc='创建时间')
    utime = db.Column(db.DateTime, default=datetime.now(), onupdate=datetime.now(), doc='更新时间')
    sequence = db.Column(db.Integer, default=0, doc='序号')
    is_visible = db.Column(db.Boolean, default=False, doc='是否可见')
    is_default = db.Column(db.Boolean, default=False, doc='是否默认')
    user = db.relationship('UserModel', secondary='user_channel', backref=db.backref('channels'))
    news = db.relationship('News', backref='channel')


# 收藏表
class Collection(db.Model):
    __tablename__ = 'collection'
    user_id = db.Column(db.Integer, db.ForeignKey('tb_user.uid'), primary_key=True, doc="用户ID")
    news_id = db.Column(db.Integer, db.ForeignKey('news_basic.nid'), primary_key=True, doc="资讯ID")
    is_delete = db.Column(db.Boolean, doc='状态(0,取消;1, 关注)')
    create_time = db.Column(db.DateTime, default=datetime.now(), doc='创建时间')
    update_time = db.Column(db.DateTime, default=datetime.now(), onupdate=datetime.now(), doc='更新时间')


# 文章基本信息表
class News(db.Model):
    __tablename__ = 'news_basic'

    class STATUS:
        DRAFT = 0  # 草稿
        UNREVIEWED = 1  # 待审核
        APPROVED = 2  # 审核通过
        FAILED = 3  # 审核失败
        DELETED = 4  # 已删除
        BANNED = 5  # 封禁

    STATUS_ENUM = [0, 1, 2, 3]
    nid = db.Column(db.Integer, primary_key=True, doc='文章ID')
    user_id = db.Column(db.Integer, db.ForeignKey('tb_user.uid'), doc='用户ID')
    channel_id = db.Column(db.Integer, db.ForeignKey('news_channel.cid'), doc='频道ID')
    title = db.Column(db.String(64), doc='标题')
    cover = db.Column(db.String(532), doc='封面')
    is_advertising = db.Column(db.Boolean, default=False, doc='是否投放广告')
    ctime = db.Column('create_time', db.DateTime, default=datetime.now(), doc='创建时间')
    status = db.Column(db.Integer, default=0, doc='帖文状态')
    reviewer_id = db.Column(db.Integer, doc='审核人员ID')
    review_time = db.Column(db.DateTime, doc='审核时间')
    delete_time = db.Column(db.DateTime, doc='删除时间')
    comment_count = db.Column(db.Integer, default=0, doc='评论数')
    good_count = db.Column(db.Integer, default=0, doc='点赞数')
    allow_comment = db.Column(db.Boolean, default=True, doc='是否允许评论')
    reject_reason = db.Column(db.String(64), doc='驳回原因')
    utime = db.Column(db.DateTime, default=datetime.now(), doc='更新时间')
    content = db.Column(db.Text, doc='帖文内容')
    is_follow = db.Column(db.Boolean, doc='是否关注', default=True)
    is_like = db.Column(db.Boolean, doc='是否点赞', default=True)
    is_collection = db.Column(db.Boolean, doc='是否收藏', default=True)
    comment = db.relationship('Comment', backref=db.backref('article'))
    user = db.relationship('UserModel', secondary='collection', backref=db.backref('collections'))


# 文章评论表
class Comment(db.Model):
    __tablename__ = 'news_comment'

    class STATUS:
        UNREVIEWED = 0  # 待审核
        APPROVED = 1  # 审核通过
        FAILED = 2  # 审核失败
        DELETED = 3  # 已删除

    cmid = db.Column(db.Integer, primary_key=True, doc='评论ID')
    user_id = db.Column(db.Integer, db.ForeignKey('tb_user.uid'), doc='用户ID')
    article_id = db.Column(db.Integer, db.ForeignKey('news_basic.nid'), doc='文章ID')
    parent_id = db.Column(db.Integer, db.ForeignKey('news_comment.cmid'), doc='被评论的评论id')
    like_count = db.Column(db.Integer, default=0, doc='点赞数')
    reply_count = db.Column(db.Integer, default=0, doc='回复数')
    content = db.Column(db.String(128), doc='评论内容')
    is_top = db.Column(db.Boolean, default=False, doc='是否置顶')
    status = db.Column(db.Integer, default=1, doc='评论状态')
    ctime = db.Column(db.DateTime, default=datetime.now(), doc='创建时间')


# 评论点赞
class CommentLiking(db.Model):
    __tablename__ = 'news_comment_liking'

    liking_id = db.Column(db.Integer, primary_key=True, doc='主键ID')
    user_id = db.Column(db.Integer, doc='用户ID')
    comment_id = db.Column(db.Integer, doc='评论ID')
    ctime = db.Column(db.DateTime, default=datetime.now(), doc='创建时间')
    is_deleted = db.Column(db.Boolean, default=False, doc='是否删除')


class SearchHistory(db.Model):
    sid = db.Column(db.Integer, primary_key=True, doc='搜索记录ID')
    content = db.Column(db.String(128), doc="用户搜索内容")
    user_id = db.Column(db.Integer, db.ForeignKey('tb_user.uid'), doc="用户ID")


